class CreatRunImg{
    private group:eui.Group;//接收的容器存放背景动画的容器
    private imgs:Array<eui.Group>;//用来存放底部背景的容器
    private stageX:number;
    private inintX:Array<number> =[ ];//记录开始滑动的时候每个背景的初始坐标
    constructor(box:eui.Group){
        this.group = box;
    }

    private createImgBox(){
        //当前有6层背景
        let hierarchy = 5; //从0开始 每个的层级
        //用数组存储每个层级的图片组对象
        let bgArr:Array<eui.Group>=[];
        //循环创建每层的图片集合
        for(let i = 0 ; i <=5 ; i++ ){
            let itemGroup:eui.Group = new eui.Group();
                itemGroup.x = 0;
                itemGroup.bottom = 0;
            if(i != 3){
                //当不是第四层的时候设置组的宽度
                itemGroup.width = 11142;
                //在当前组中创建图片,并将图片位置摆放好,每个组会有六张图片,因为需要无缝滑动,所以需要首位各加上一张
                let firstImg:eui.Image = new eui.Image();
                let endImg:eui.Image  = new eui.Image();
                let endx:number = 0;
                firstImg.source = `./resource/assets/foreground_${i+1}/foreground_${i+1}_5.png`
                endImg.source  = `./resource/assets/foreground_${i+1}/foreground_${i+1}_1.png`
                itemGroup.addChild(firstImg);
                firstImg.x = 0;
                firstImg.bottom = 0;
                for(let j = 0 ; j < 5 ; j ++){
                    let imgItem:eui.Image = new eui.Image();
                    imgItem.source = `./resource/assets/foreground_${i+1}/foreground_${i+1}_${j+1}.png`
                    itemGroup.addChild(imgItem);
                    imgItem.x = j * 2046 + 456;
                    imgItem.bottom = 0;
                }
                itemGroup.addChild(endImg)
                endImg.x = 9096;
            }else{
                itemGroup.width = 3894;
                let firstImg:eui.Image = new eui.Image();
                let middleImg:eui.Image = new eui.Image();
                let endImg:eui.Image = new eui.Image();
                itemGroup.addChild(firstImg);
                itemGroup.addChild(middleImg);
                itemGroup.addChild(endImg);
                firstImg.source  = `./resource/assets/foreground_${i+1}/foreground_${i+1}_1.png`;
                middleImg.source  = `./resource/assets/foreground_${i+1}/foreground_${i+1}_1.png`;
                endImg.source  = `./resource/assets/foreground_${i+1}/foreground_${i+1}_1.png`;
                firstImg.x = 0;
                firstImg.bottom = 0;
                middleImg.x = 1298;
                middleImg.bottom = 0;
                endImg.x = 2596;
                endImg.bottom = 0;
            }

            bgArr.push(itemGroup);
        }
        this.imgs = bgArr.reverse();
    }

    public addMoveBg(){
        this.createImgBox() ;
        SK_GLOBAL.curStage().addEventListener(egret.Event.ENTER_FRAME,this.frameEvts,this);
        this.imgs.forEach((v,i)=>{
            this.group.addChild(v);
        })
    }
    //通过滑动方向,滚动底部背景;
    public moveStart(stageX:number):void{
        //停掉帧监听,页面禁止
         SK_GLOBAL.curStage().removeEventListener(egret.Event.ENTER_FRAME,this.frameEvts,this);
         this.stageX = stageX;
         this.imgs.forEach((v,i)=>{
             this.inintX[i] = v.x;
         });
    }
    //移动中
    public isOnMove (stageX:number):void{
        // console.log(stageX -this.stageX);
        this.imgs.forEach((v,i)=>{
            v.x = this.inintX[i] + (stageX -this.stageX)/(6-i);
            if(i==2){
                if(v.x <= -1298){
                    v.x = v.x - 1298;
                    this.inintX[i] = v.x + (stageX -this.stageX)/(6-i);
                }else if(v.x > 0){
                    v.x =v.x -1298;
                    this.inintX[i] = v.x + (stageX -this.stageX)/(6-i);
                }
            }else{
                if(v.x <= -8640){
                    v.x = v.x - 8640;
                    this.inintX[i] = v.x + (stageX -this.stageX)/(6-i);
                }else if(v.x > 0){
                    v.x = v.x - 8640;
                    this.inintX[i] = v.x + (stageX -this.stageX)/(6-i);
                }
            }
        })
    }
    //移动完成
    public moveOver(status:string):void{
        //动画效果
        let speed = 1;
        let animationEnd:Array<any> = [];
        switch (status){
            case 'next':
                speed *= -1;
                break;
            case 'prave':
                speed == 1;
                break;
            case 'now':
                speed = 0;
                break;
        }
        this.imgs.forEach((v,i)=>{
            // 缓动动画移动到指定位置
            // 目标位置
            let active = this.inintX[i]+2046*speed/(6-i);
            let p = new Promise((resolve,reject)=>{
                egret.Tween.get(v).to({x:active, ease: egret.Ease.quadOut},300).call(()=>{
                    resolve();
                })
            })
        })
        Promise.all(animationEnd).then(()=>{
            SK_GLOBAL.curStage().addEventListener(egret.Event.ENTER_FRAME,this.frameEvts,this);
        })
    }
    private frameEvts():void{
        this.imgs.forEach((v,i)=>{
            let dely:number = 1;
            dely = i+1;
            if(i ==2) dely = 2;
            v.x -= dely;
            if(i==2){
                if(v.x <= -1298){
                    v.x = 0;
                }
            }else{
                if(v.x <= -8640){
                    v.x = 0;
                }
            }
        })
    }
}   